﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 女友管理系统
{
    public partial class Form1 : Form
    {
        string s;
        public Form1()
        {
            InitializeComponent();
            s = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            s = "";
            s += textBox1.Text;
            s += " ";
            s += dateTimePicker1.Text;
            s += " ";
            s += comboBox1.Text;

            listBox1.Items.Add(s);
            trackBar1.Maximum = listBox1.Items.Count;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //trackBar1.Value = listBox1.SelectedIndex;
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            // 两个函数冲突啦
            //if (listBox1.SelectedIndex != -1)
            //{
            //    int i = trackBar1.LargeChange;
            //    object tmp = listBox1.SelectedItem;
            //    int iselect = listBox1.SelectedIndex;
            //    listBox1.Items.Insert(iselect + i, tmp);
            //    listBox1.Items.RemoveAt(iselect - i);
            //}
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                //int i = trackBar1.SmallChange;
                int i = trackBar1.Value - listBox1.SelectedIndex;
                object tmp = listBox1.SelectedItem;
                int iselect = listBox1.SelectedIndex;
                
                if (i > 0 )//&& iselect + i + 1 < listBox1.Items.Count)
                {
                    if(iselect + i + 1 < listBox1.Items.Count)
                    {
                        listBox1.Items.Insert(iselect + i + 1, tmp);
                        listBox1.Items.RemoveAt(iselect);
                    }
                    else if(iselect + i + 1 == listBox1.Items.Count)
                    {
                        listBox1.Items.Add((string)tmp);
                        listBox1.Items.RemoveAt(iselect);
                    }
                }
                else if(i < 0 &&  iselect + i  > 0)
                {
                    listBox1.Items.Insert(iselect + i, tmp);
                    listBox1.Items.RemoveAt(iselect - i);
                    
                }
            }         
        }

        private void listBox1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                trackBar1.Value = listBox1.SelectedIndex;
            }
            
        }
    }
}
